package com.aaa.wash.service.impl;

import com.aaa.common.entity.WashCarCoupon;
import com.aaa.common.util.TokenUtil;
import com.aaa.wash.dao.WashCarCouponDao;
import com.aaa.wash.service.WashCarCouponService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (WashCarCoupon)表服务实现类
 *
 * @author makejava
 * @since 2025-10-13 10:50:07
 */
@Service("washCarCouponService")
public class WashCarCouponServiceImpl implements WashCarCouponService {
    @Resource
    private WashCarCouponDao washCarCouponDao;
    @Resource
    private HttpServletRequest request;

    @Override
    public List<WashCarCoupon> selectCarWashCoupon(Integer storeId) {
        String userId = TokenUtil.getUserId(request);
        return washCarCouponDao.selectCarWashCoupon(storeId, Integer.valueOf(userId));
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WashCarCoupon queryById(Integer id) {
        return this.washCarCouponDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<WashCarCoupon> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        WashCarCoupon washCarCoupon = page.getData();
        return new PageInfo(washCarCouponDao.queryAllByLimit(washCarCoupon));
    }

    /**
     * 新增数据
     *
     * @param washCarCoupon 实例对象
     * @return 实例对象
     */
    @Override
    public WashCarCoupon insert(WashCarCoupon washCarCoupon) {
        this.washCarCouponDao.insert(washCarCoupon);
        return washCarCoupon;
    }

    /**
     * 修改数据
     *
     * @param washCarCoupon 实例对象
     * @return 实例对象
     */
    @Override
    public WashCarCoupon update(WashCarCoupon washCarCoupon) {
        this.washCarCouponDao.update(washCarCoupon);
        return this.queryById(washCarCoupon.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.washCarCouponDao.deleteById(id) > 0;
    }
}
